package com.five.controller;

import com.five.dto.AuthorUpdateRequest;
import com.five.dto.IdRequest;
import com.five.dto.ReplyRequest;
import com.five.exception.BusinessException;
import com.five.pojo.Author;
import com.five.pojo.Novel;
import com.five.result.Result;
import com.five.service.AuthorInteractionService;
import com.five.service.AuthorProfileService;
import com.five.service.AuthorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/author")
@Slf4j
public class AuthorController {
    @Autowired
    private AuthorService authorService;
    @Autowired
    private AuthorInteractionService interactionService;
    @Autowired
    private AuthorProfileService profileService;

    /**
     * 根据手机号查询作者信息
     * @param phone
     * @return
     */
    @PostMapping("queryByPhone/{phone}")
    public Result<Author> queryByPhone(@PathVariable String phone){
        try {
            Author author = authorService.queryByPhone(phone);
            return Result.success(author);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }


    /**
     * 根据邮箱查询作者信息
     * @param email
     * @return
     */
    @PostMapping("queryByEmail/{email}")
    public Result<Author> queryByEmail(@PathVariable String email){
        try {
            Author author = authorService.queryByEmail(email);
            return Result.success(author);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 根据登陆信息Id查询作者信息
     * @param loginInfoId
     * @return
     */
    @PostMapping("queryByLoginInfoId/{loginInfoId}")
    public Result<Author> queryByLoginInfoId(@PathVariable Long loginInfoId){
        try {
            Author author = authorService.queryByLoginInfoId(loginInfoId);
            return Result.success(author);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 插入作者信息
     * @param
     * @return
     */
    @PostMapping("insertAuthor")
    public Result insertAuthor(@RequestBody Author author){
        try {
           authorService.insertAuthor(author);
           return Result.success("新增作者成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     *更新作者信息
     * @param
     * @return
     */
    @PostMapping("updateAuthor")
    public Result updateAuthor(@RequestBody Author author){
        try {
            authorService.updateAuthor(author);
            return Result.success("更新作者信息成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     *根据账号查询作者信息
     * @param
     * @return
     */
    @PostMapping("queryByAccount/{account}")
    public Result<Author> queryByAccount(@PathVariable String account){
        try {
            Author author = authorService.queryByAccount(account);
            return Result.success(author);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 互动管理 - 获取作者所有书籍及评论
     */
    @PostMapping("/books/comments")
    public Result<List<Novel>> getBooksWithComments(@RequestBody IdRequest request) {
        try {
            List<Novel> data = interactionService.getAuthorBooksWithComments(request.getAuthorId());
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取书籍评论失败", e);
            return Result.error("获取书籍评论失败: " + e.getMessage());
        }
    }

    /**
     * 互动管理 - 作者回复评论
     */
    @PostMapping("/reply")
    public Result<Void> replyToComment(@RequestBody ReplyRequest request) {
        try {
            interactionService.replyToComment(request);
            return Result.success("回复成功");
        } catch (Exception e) {
            log.error("回复评论失败", e);
            return Result.error("回复失败: " + e.getMessage());
        }
    }

    /**
     * 互动管理 - 删除作者评论
     */
    @PostMapping("/delete-interaction")
    public Result<Void> deleteInteraction(@RequestBody IdRequest request) {
        try {
            interactionService.deleteInteraction(request.getInteractionId());
            return Result.success("删除成功");
        }catch (Exception e){
            log.error("删除评论失败", e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 个人中心 - 获取作者个人信息
     */
    @PostMapping("/profile")
    public Result<Author> getAuthorProfile(@RequestBody IdRequest request) {
        try {
            Author profile = profileService.getAuthorProfile(request.getAuthorId());
            if (profile == null) {
                return Result.error("作者不存在");
            }
            return Result.success(profile);
        } catch (Exception e) {
            log.error("获取作者信息失败", e);
            return Result.error("获取作者信息失败: " + e.getMessage());
        }
    }

    /**
     * 个人中心 - 更新作者信息
     */
    @PostMapping("/update-profile")
    public Result<Void> updateAuthorProfile(@RequestBody AuthorUpdateRequest request) {
        try {
            profileService.updateAuthorProfile(request);
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新作者信息失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

}
